{% extends "layout.html" %}
{% block css %}
 <style>.form-horizontal select.m-b{padding-top: 3px}</style>
{% endblock %}
{% block content %}
{{ include('modalwindow.html') }}
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>课程管理 <small> 新增课程</small></h5>
                        <div class="ibox-tools">
                        </div>
                    </div>
                    <div class="col-xs-5 col-md-5">
                        <a href="{{url('/teacher/index.html')}}"><button type="button" class="btn btn-w-m btn-success"><i class="fa fa-mail-reply"></i> 返回列表</button></a>
                        <a onClick="javascript:window.location.reload();"><button type="button" class="btn btn-w-m btn-info"><i class="fa fa-undo"></i> 刷新页面</button></a>
                    </div>
                    <div class="ibox-content">
                        <form class="form-horizontal m-t" id="courseForm">
                            <input id="teacher_id" name="teacher_id" value="{{teacher.id}}" class="form-control" type="hidden">
                            <div class="form-group">
                                <label class="col-sm-3 control-label">授课教师：</label>
                                <div class="col-sm-7">
                                    <input id="teacher_name" value="{{teacher.name}}" disabled="disabled" class="form-control" type="text">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">上课学期：</label>
                                <div class="col-sm-7">
                                   <select id="semester_id" class="form-control m-b" name="semester_id">
                                        <option value="">请选择课程学期</option>
                                        {% if semesters|length > 0 %}
                                            {% for semester in semesters %}
                                                <option value="{{ semester.id|e }}">{{ semester.name|e }}</option>
                                            {% endfor %}
                                        {% endif %}
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">课程名称：</label>
                                <div class="col-sm-7">
                                   <select id="subject_id" class="form-control m-b" name="subject_id">
                                        <option value="">请选择课程名称</option>
                                        {% if subjects|length > 0 %}
                                            {% for subject in subjects %}
                                                <option value="{{ subject.id|e }}">[ {{subject.credit}} 学分] {{ subject.name|e }}</option>
                                            {% endfor %}
                                        {% endif %}
                                    </select>
                                </div>
                            </div>
                            <div class="form-group" {{W(33)}} >
                                <label class="col-sm-3 control-label">上课星期：</label>
                                <div class="col-sm-7">
                                    <select id="week_id" class="form-control m-b" name="week_id">
                                        <option value="0">请选择上课星期</option>
                                        {% if weeks|length > 0 %}
                                            {% for week in weeks %}
                                                <option value="{{ week.id|e }}"> {{ week.name|e }}</option>
                                            {% endfor %}
                                        {% endif %}
                                    </select>
                                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 可选，由授课教师通知</span>
                               </div>
                            </div>
                            <div class="form-group" {{W(33)}}>
                                <label class="col-sm-3 control-label">上课时间：</label>
                                <div class="col-sm-7">
                                   <select id="interval_id" class="form-control m-b" name="interval_id">
                                        <option value="0">请选择上课时间</option>
                                        {% if intervals|length > 0 %}
                                            {% for interval in intervals %}
                                                <option value="{{ interval.id|e }}"> {{ interval.name|e }}</option>
                                            {% endfor %}
                                        {% endif %}
                                    </select>
                                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 可选，由授课教师通知</span>
                                </div>
                            </div>
                            <div class="form-group" {{W(33)}}>
                                <label class="col-sm-3 control-label">上课教室：</label>
                                <div class="col-sm-7">
                                    <select id="room_id" class="form-control m-b" name="room_id">
                                        <option value="0">请选择上课教室</option>
                                        {{ rooms | raw }}
                                    </select>
                                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 可选，由授课教师通知</span>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class=" col-sm-3 col-sm-offset-3">
                                    <button class="btn btn-default" type="reset">重置</button>
                                    <button class="btn btn-primary" type="submit">提交</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
{% endblock %}
{% block script %}
<!-- jQuery Validation plugin javascript-->
<script src="{{url('/assets/js/plugins/validate/jquery.validate.min.js')}}"></script>
<script src="{{url('/assets/js/plugins/validate/messages_zh.min.js')}}"></script>
<script src="{{url('/assets/js/plugins/layer/layer.min.js')}}"></script>

<script>
$(function($) {
    
        // 表单验证
        var icon = "<i class='fa fa-times-circle'></i> ";
        var validate = $("#courseForm").validate({
            debug: false, //调试模式取消submit的默认提交功能 
            rules: {
                semester_id:{required: true},
                subject_id:{required: true}
               // week_id: {required: true},
                //interval_id: {required: true}
            },
            messages: {
                semester_id: {required: icon + "请选择学期"},
                subject_id: {required: icon + "请选择课程"}
                //week_id: {required: icon + "请选择上课星期"},
                //interval_id: {required: icon + "请选择上课时间段"}
            },
            submitHandler: function(form){   //表单提交句柄,为一回调函数，带一个参数：form   
                var datas = $(form).serialize();
                $.ajax({
                    type: "POST",
                    url: "{{ url('/course/adddata.html') }}",
                    data: datas,
                    dataType: "json",
                    success: function(dataJson){
                        if(dataJson.valid === 0){
                           var message = dataJson.message;
                           $.each(message,function(i,v){  // 后端验证提示
                               var error ='<span class="help-block m-b-none"><i class="fa fa-times-circle"></i> '+v+'</span>';
                               $('#'+i).after(error);
                           });
                       }else{
                           text = 'error';
                           if(dataJson.status == 500102){
                              text = 'success';
                           }
                           $.AngelaAutoTips({
                               msg : dataJson.message, //显示的消息
                               type : text		   //提示类型（1、success 2、error 3、warning）
                           });
                       }
                    },
                    error:function(){
                        $.AngelaAutoTips({
                            msg : ' 很抱歉！新增失败，请核实信息是否正确', //显示的消息
                            type : 'error'		   //提示类型（1、success 2、error 3、warning）
                        });
                    }
                });
            }
        });  // 表单验证
    });
</script>
{% endblock %}